/**
 * 
 */
package org.vectrics.recruiter.struts.admin.person;

import java.util.List;

import javax.servlet.jsp.JspException;

import org.apache.struts.action.ActionMessages;
import org.vectrics.cv.Career;
import org.vectrics.cv.Studies;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobFunction;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

/**
 * @author pgoette
 * Action encargado de borrar una carrera
 */
public class DeleteCareerAction extends VectricsStrutsAction {


	@Override
	protected String doExecute(VectricsActionData actionData)
			throws JspException {
		
		String forward = "success";
		String idCareer = actionData.getRequest().getParameter("careerId");
		
        if ((idCareer!=null) && (!idCareer.trim().equals(""))) {
        	// Busco la carrera segun el id.
        	Career career = RecruitServiceLocator.getPersonService().findCareer(new Long(idCareer));
			// Recupero los estudios
        	List<Studies> studies = RecruitServiceLocator.getPersonService().findStudies();
			
        	for (Studies study : studies){
        		if (study.getCareer().equals(career)) {
        			actionData.addError(ActionMessages.GLOBAL_MESSAGE, "error.delete.person.career");        			
        			return forward;
        		}
        	}
        	
			RecruitServiceLocator.getPersonService().deleteCareer(career);
		} 
		
		return forward;
	}

}
